package me.frank.security.config;

import me.frank.security.authentication.MyAuthenticationFailureHandler;
import me.frank.security.authentication.MyAuthenticationSuccessHandler;
import me.frank.security.filter.ValidateCodeFilter;
import me.frank.security.properties.SecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @Author Frank Wang
 * @Description:
 * @Date 2018/12/10 15:26
 */
@Configuration
@EnableConfigurationProperties(SecurityProperties.class)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private MyAuthenticationSuccessHandler myAuthenticationSuccessHandler;

    @Autowired
    private MyAuthenticationFailureHandler myAuthenticationFailureHandler;

    @Override
    public void configure(WebSecurity web) throws Exception {
        super.configure(web);
    }

    @Autowired
    private ValidateCodeFilter validateCodeFilter;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

                http.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class)
                .formLogin().loginPage("/login.html")
                .loginProcessingUrl("/authentication/form")
                .successHandler(myAuthenticationSuccessHandler)
                .failureHandler(myAuthenticationFailureHandler)
                .and()
                .authorizeRequests()
                .antMatchers("/login.html",
                        "/code/image",
                        "/swagger*",
                        "/user/*",
                        "/webjars/**",
                        "/*/swagger-resources",
                        "/swagger-resources/configuration/ui",
                        "/webjars/springfox-swagger-ui/**/**",
                        "/webjars/springfox-swagger-ui/css/**",
                        "/webjars/springfox-swagger-ui/images/**",
                        "/webjars/springfox-swagger-ui/**",
                        "/swagger-resources/configuration/security",
                        "/v2/api-docs",
                        "/swagger-ui.html").permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .csrf().disable();
        //super.configure(http);
    }

    @Bean
    @Qualifier
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
}
